package com.pcbly.web.action;

import java.util.List;

import com.pcbly.web.bean.client.CustomerBean;
import com.pcbly.web.bean.user.UserBean;
import com.pcbly.web.business.client.CustomerService;
import com.pcbly.web.business.user.UserService;

@SuppressWarnings("unchecked")
public class CommonAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8075956089166815117L;

	private String name;

	private String tip;

	private CustomerService customerService;

	private UserService userService;

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String searchJsr() throws Exception {
		try {
			List<UserBean> userBeans=userService.findUserBeanByName(name);
			
			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());

			return ERROR;
		}
	}

	public String searchClients() throws Exception {
		try {
			List<CustomerBean> customerBeans = customerService
					.findCustomerBeansByName(name);
			if (customerBeans != null && !customerBeans.isEmpty()) {
				StringBuilder sb = new StringBuilder();
				for (CustomerBean customerBean : customerBeans) {
					sb.append(customerBean.getId());
					sb.append("$");
					sb.append(customerBean.getClientName());
					sb.append("%");
				}
				int leng = sb.length();
				if (leng > 1) {
					tip = sb.substring(0, leng - 1);
				}
			}
			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());

			return ERROR;
		}
	}

}
